<template>
	<view class="">
		<view class="withdrawal">
			<view class="title_1">
				<view class="card_1">
					提现银行卡
				</view>
				<view class="card_2" @tap.stop="toMoney">
					添加银行卡
				</view>
			</view>
			<view class="title_2" @tap.stop="toCard" v-if="card">
				<view class="" style="width: 76%;">
					<view class="name">
						{{card.name}}
					</view>
					<view class="num">
						{{card.number}}
					</view>
				</view>
				<view class="" style="margin-right: 20rpx;padding: 50rpx 18rpx 50rpx 90rpx;">
					<u-icon name="arrow-right" size="16" color="#B6B6B6"></u-icon>
				</view>
			</view>
			<view class="title_2" @tap.stop="toCard" v-else>
				<view class="" style="width: 76%;padding-left: 50rpx;">
					请选择银行卡
				</view>
				<view class="" style="margin-right: 20rpx;padding: 50rpx 18rpx 50rpx 90rpx;">
					<u-icon name="arrow-right" size="16" color="#B6B6B6"></u-icon>
				</view>
			</view>
		</view>

		<view class="withdrawal">
			<view class="title">请输入提现金额</view>
			<view class="inputBox ">
				<view class="symbol">¥</view>
				<input type="number" v-model="withdrawalPrice" @blur="mes">
			</view>
			<view class="now">
				<view class="text" style="margin: 0;">当前可提现金额为{{total||0}}元，</view>
				<view class="" style="color: #3DAD81;" @click="allGet">
					全部提现
				</view>
			</view>
			<view class="btn1" @tap.stop="withdrawalBtn">确定</view>
		</view>
	</view>
</template>

<script>
	import {
		commissionNow,
	} from "@/request/homeApi.js";
	import {
		withdraw,
	} from "@/request/myApi.js";
	// import base from '@/api/base.js'
	export default {
		data() {
			return {
				action: 'http://www.example.com/upload',
				total: '',
				withdrawalPrice: '',
				withdrawal_interdict: '',
				imgUrl: '',
				card: {},
				total: ''
			};
		},
		onLoad(e) {
			console.log(e, 'eeeee')
			this.total = e.total
			// this.withdrawal_interdict = el.withdrawal_interdict
			this.getUserInfo()

			// this.getRules()
		},
		onShow() {
			this.card = uni.getStorageSync('card') ? JSON.parse(uni.getStorageSync('card')): ''
			console.log(this.card, 'card')
		},
		methods: {
			mes(e) {
				console.log(e.detail.value, 'eee', this.total)
				if (e.detail.value > this.total) {
					uni.showToast({
						title: "超过最大可提现数额",
						icon: "none"
					})
					return
				}
			},
			// 获取用户信息
			getUserInfo() {
				let that = this
				commissionNow().then(res => {
					if (res.code == 200) {
						this.total = res.data
					}
					console.log(res, 'data')
				})
			},
			onRemove(index, lists) {
				this.imgUrl = ''
			},
			allGet() {
				this.withdrawalPrice = this.total
				// setTimeout(()=>{
				// 	this.withdrawalBtn()
				// },2000)
			},
			submit() {
				this.$refs.uUpload.upload();
			},
			toMoney() {
				uni.navigateTo({
					url: '/pages/home/applyDetails'
				})
			},
			toCard() {
				uni.navigateTo({
					url: '/pages/home/living'
				})
			},
			async toSuccess(e) {
				let getavatarUrl = await this.uploadFilePromise(e[0].url)
				this.imgUrl = getavatarUrl.showUrl
				// console.log(this.imgUrl,'eeeeefdddddddddddddd')

			},
			uploadFilePromise(avatarUrl) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `${base.imgUrl}/common/upload`, // 仅为示例，非真实的接口地址
						filePath: avatarUrl,
						name: 'file',
						formData: {
							user: 'test',
						},
						success: (res) => {
							// console.log(JSON.parse(res.data),'eeeeefdddddddddddddd')
							setTimeout(() => {
								resolve({
									url: JSON.parse(res.data).url,
									showUrl: JSON.parse(res.data).fileName,
								})
							}, 1000)
						},
						fail(fails) {
							console.log(fails, 'fail')
						}
					});
				})
			},
			withdrawalBtn() {
				let that = this
				if (that.withdrawalPrice == 0) {
					
					return
				}
				if (that.withdrawalPrice == '') {
					uni.showToast({
						title: "请输入提现金额",
						icon: "none"
					})
					return
				}
				console.log(that.card == '','kkkkkkk')
		
				if (that.card == '') {
					uni.showToast({
						title: "请选择银行卡",
						icon: "none"
					})
					return
				}
				if (that.withdrawalPrice > that.total) {
					uni.showToast({
						title: "超过最大可提现数额",
						icon: "none"
					})
					return
				}
				withdraw({
					bank_id: that.card.id,
					title: '',
					price: that.withdrawalPrice,
					type: 2
				}).then(res => {
					uni.showToast({
						title: res.msg,
						icon: "none"
					})

					if (res.code == 200) {
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							});
						}, 1500);
					}
					console.log(res, 'res')
				})
			}
		}
	}
</script>

<style lang="less">
	::v-deep .u-upload u-list-item u-add-wrap {
		width: 100rpx;
		height: 100rpx;
	}

	.withdrawal {
		background: #F8F8F8;
		width: 100vw;
		width: 92%;
		margin: 30rpx auto;
		border-radius: 20rpx;
		padding: 30rpx 0;
		padding-top: 10rpx;

		.name,
		.num {
			height: 40rpx;
			line-height: 40rpx;
			color: #000;
			margin: 0rpx auto;
			width: 90%;
			margin-top: 30rpx;
			font-size: 30rpx;
		}

		.name {
			font-weight: 600;
		}

		.now {
			display: flex;
			justify-content: flex-start;
			padding: 0rpx 40rpx;
			font-size: 28rpx;

			.text {
				margin: 40rpx auto;
				font-weight: 400;
				color: #000;
			}

		}

		.title_2 {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.title_1 {
			width: 90%;
			margin: 20rpx auto;
			font-size: 28rpx;
			font-weight: 400;
			color: #58B99D;
			display: flex;
			justify-content: space-between;
			margin-bottom: 40rpx;

			.card_1 {
				color: #979797;
			}

			.card_2 {}
		}

		.title {
			width: 90%;
			margin: 20rpx auto;
			font-size: 28rpx;
			font-weight: 400;
			color: #58B99D;
		}

		.inputBox {
			width: 90%;
			margin: 30rpx auto;
			border-bottom: 1px solid #EFEFEF;
			display: flex;
			padding-bottom: 20rpx;

			.symbol {
				font-size: 50rpx;
				font-weight: 500;
				color: #000;
				margin-right: 20rpx;
			}

			input {
				height: 70rpx;
				font-size: 70rpx;
			}
		}

		.btn1 {
			width: 90%;
			height: 96rpx;
			text-align: center;
			line-height: 96rpx;
			// border-radius: 96rpx;
			border-radius: 20rpx;
			background-color: #58B99D;
			color: #fff;
			font-size: 28rpx;
			font-weight: 500;
			position: fixed;
			left: 50%;
			bottom: 400rpx;
			transform: translateX(-50%);
		}
	}
</style>